package ben.samplegame.sprite;

import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.KeyStroke;
import ben.samplegame.rules.DeathRule;
import ben.samplegame.rules.GravityRule;
import ben.samplegame.rules.OutOfBoundsRule;
import ben.samplegame.rules.SpawnRule;
import game.engine.GameLevel;
import game.engine.GameRule;
import game.physics.GameVector;
import game.sprite.PlayerSprite;
import game.sprite.Sprite;
import game.userinteraction.IControllable;
import game.userinteraction.UserInputBehavior;

import game.userinteraction.UserInputHandler;

public class SampleLevel extends GameLevel implements IControllable {
    private UserInputBehavior myBehavior;
    private boolean myWon = false;
    private boolean myLost = false;

    public SampleLevel (File levelInfo, List<GameRule> levelRules, UserInputHandler inputHandler) throws SecurityException, NoSuchMethodException {
        super(levelInfo, levelRules, inputHandler);
        myBehavior = new UserInputBehavior(this);
        myBehavior.defineKeyBindings(ResourceBundle.getBundle("resources.LevelActions"));
        temporaryCreation();
        inputHandler.add((SamplePlayer)getPlayer());
        inputHandler.add(this);





    }

    private void temporaryCreation() throws SecurityException, NoSuchMethodException {
        Image testBackground = null;
        try {
            testBackground = ImageIO.read(new File("src/ben/samplegame/resources/blackhole_background.jpeg"));

        } catch (IOException e) {
        }
        SpaceBackground background = new SpaceBackground(testBackground, 600, 600, 0, 0);
        PlayerSprite player = new SamplePlayer(new Point(0,200), "", true);

        setBackground(background);

        setPlayer(player);
        ArrayList<Sprite> levelSprites = new ArrayList<Sprite>();
        levelSprites.add(player);


        setLevelSprites(levelSprites);
        ArrayList<GameRule> levelRules = new ArrayList<GameRule>();
        levelRules.add(new GravityRule(levelSprites, background));
        levelRules.add(new SpawnRule(this));
        levelRules.add(new OutOfBoundsRule(getLevelSprites(),background));
        levelRules.add(new DeathRule(getPlayer(),this));
        setLevelRules(levelRules);

        SampleEnemy one =  new SampleEnemy(new Point(200, 500),"", true, getPlayer());
        SampleEnemy two = new SampleEnemy(new Point(100, 500),"", true, getPlayer());
        SampleEnemy three = new SampleEnemy(new Point(400, 500),"", true, getPlayer());
        addCollidable(one);
        addCollidable(two);
        addCollidable(three);
        addSprite(one);
        addSprite(two);
        addSprite(three);





    }



    @Override
    public void react (Set<KeyStroke> keyPresses) {

        myBehavior.react(keyPresses);
    }

    public void shoot(){
        // System.out.println("shoot");
        SampleBullet newBullet = new SampleBullet(getPlayer().getCenterCoordinate(), "", true);
        addSprite(newBullet);
        GameVector shootForce = new GameVector(0,10);
        shootForce.rotate(getPlayer().getRotation());
        newBullet.addExternalForce(shootForce);
        addCollidable(newBullet);


    }

    public void spawnEnemy () {
        SampleEnemy one =  new SampleEnemy(new Point(400, 500),"", true, getPlayer());
        addCollidable(one);
        addSprite(one);

    }

    public void setWon (boolean b) {
        myWon = b;

    }

    public void setLost (boolean b) {
        myLost = b;

    }
    public boolean getIsWon(){
        return myWon;
    }
    public boolean getIsLoss(){
        return myLost;
    }








}
